{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b5592414-c91d-4d1d-862a-bba3efb0be2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dotenv:  E:\\work\\code\\ai\\.env\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "\n",
    "def get_openai_key():\n",
    "    print(\"dotenv: \",find_dotenv())\n",
    "    _ = load_dotenv(find_dotenv())\n",
    "    # api_key = os.environ['OPENAI_API_KEY']\n",
    "    return OpenAI(\n",
    "        api_key = os.environ.get(\"ARK_API_KEY\"),\n",
    "    base_url = \"https://ark.cn-beijing.volces.com/api/v3\",)\n",
    "\n",
    "client = get_openai_key()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "815a866f-1c2d-40aa-bdf1-50a8844c6218",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_completion(prompt, model=\"ep-20250103110319-28xth\",temperature = 0):\n",
    "    \n",
    "    completion = client.chat.completions.create( \n",
    "    model = model,  # your model endpoint ID\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": \"你是豆包，是由字节跳动开发的 AI 人工智能助手\"},\n",
    "        {\"role\": \"user\", \"content\": prompt},\n",
    "    ],\n",
    "    temperature=temperature\n",
    "    )\n",
    "    return completion.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "51332474-497b-4dc7-971c-abccf70f2fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 我们可以在推理那章学习到如何对一个评论判断其情感倾向\n",
    "sentiment = \"积极的\"\n",
    "# 一个产品的评价\n",
    "review = f\"\"\"\n",
    "他们在11月份的季节性销售期间以约49美元的价格出售17件套装，折扣约为一半。\\\n",
    "但由于某些原因（可能是价格欺诈），到了12月第二周，同样的套装价格全都涨到了70美元到89美元不等。\\\n",
    "11件套装的价格也上涨了大约10美元左右。\\\n",
    "虽然外观看起来还可以，但基座上锁定刀片的部分看起来不如几年前的早期版本那么好。\\\n",
    "不过我打算非常温柔地使用它，例如，\\\n",
    "我会先在搅拌机中将像豆子、冰、米饭等硬物研磨，然后再制成所需的份量，\\\n",
    "切换到打蛋器制作更细的面粉，或者在制作冰沙时先使用交叉切割刀片，然后使用平面刀片制作更细/不粘的效果。\\\n",
    "制作冰沙时，特别提示：\\\n",
    "将水果和蔬菜切碎并冷冻（如果使用菠菜，则轻轻煮软菠菜，然后冷冻直到使用；\\\n",
    "如果制作果酱，则使用小到中号的食品处理器），这样可以避免在制作冰沙时添加太多冰块。\\\n",
    "大约一年后，电机发出奇怪的噪音，我打电话给客服，但保修已经过期了，所以我不得不再买一个。\\\n",
    "总的来说，这些产品的总体质量已经下降，因此它们依靠品牌认可和消费者忠诚度来维持销售。\\\n",
    "货物在两天内到达。\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2ff77b79-cb9e-46c4-a828-1c63b604604f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "主题：感谢您的关注\n",
      "\n",
      "尊敬的客户：\n",
      "\n",
      "您好！\n",
      "\n",
      "非常感谢您对我们产品的关注与支持。我们留意到您提到在11月季节性销售期间，以约49美元的价格购买到17件套装，折扣约为一半，这确实是很实惠的价格。同时也感谢您分享了在使用产品过程中的诸多实用技巧，比如制作冰沙时对食材的处理方式等，相信这会对其他消费者有很大的帮助。\n",
      "\n",
      "另外，您反馈货物在两天内就到达了，我们很高兴能在物流配送方面为您提供较为高效的服务。\n",
      "\n",
      "再次感谢您的支持与喜爱，期待未来能继续为您提供优质的产品和服务。\n",
      "\n",
      "AI客户代理\n",
      "\n",
      "XXXX年XX月XX日 \n"
     ]
    }
   ],
   "source": [
    "prompt =f\"\"\" \n",
    "你是一位客户服务的AI助手。\n",
    "你的任务是给一位重要的客户发送邮件回复。\n",
    "通过客户评论，生产回复客户的评价;如果客户评论情感是积极的，回复感谢客户的关注; 如果客户回复是负面的，告诉客户可以联系客户反馈遇到的问题。\n",
    "提醒模型使用评价中的具体细节用简明而专业的语气写信。\n",
    "作为\"AI客户代理\"签署电子邮件。\n",
    "客户评论：\n",
    "```{review}```\n",
    "评论情感：{sentiment}\n",
    "\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c15006af-2ae8-4f32-ae4f-4f65ee5ed045",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "主题：感谢您的关注\n",
      "\n",
      "尊敬的客户：\n",
      "\n",
      "非常感谢您对我们产品的关注和详细的反馈。我们很欣慰您提到货物能在两天内到达，这体现了我们在物流配送方面的努力能让您及时收到商品。同时，感谢您分享了使用产品的诸多技巧，例如制作冰沙时的特别提示，这对其他用户也会很有帮助。\n",
      "\n",
      "再次感谢您的支持与关注，期待未来能继续为您服务。\n",
      "\n",
      "AI客户代理\n"
     ]
    }
   ],
   "source": [
    "prompt =f\"\"\" \n",
    "你是一位客户服务的AI助手。\n",
    "你的任务是给一位重要的客户发送邮件回复。\n",
    "通过客户评论，生产回复客户的评价;如果客户评论情感是积极的，回复感谢客户的关注; 如果客户回复是负面的，告诉客户可以联系客户反馈遇到的问题。\n",
    "提醒模型使用评价中的具体细节用简明而专业的语气写信。\n",
    "作为\"AI客户代理\"签署电子邮件。\n",
    "客户评论：\n",
    "```{review}```\n",
    "评论情感：{sentiment}\n",
    "\n",
    "\"\"\"\n",
    "response = get_completion(prompt,temperature=0.7)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7fc1e9fc-39d1-4651-8189-bc431b6f338a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "主题：感谢您的关注\n",
      "\n",
      "尊敬的客户：\n",
      "\n",
      "您好！非常感谢您对我们产品的关注和详细的反馈。我们很高兴得知货物能在两天内顺利到达您手中，为您带来了便捷的体验。\n",
      "\n",
      "同时，也感谢您分享了关于产品使用的诸多宝贵心得，例如在制作冰沙时的特别提示等，这些内容对其他用户也会有很大的帮助。\n",
      "\n",
      "再次感谢您的支持与信任，我们会持续努力，为您提供更优质的产品和服务。\n",
      "\n",
      "祝您生活愉快！\n",
      "\n",
      "AI客户代理\n"
     ]
    }
   ],
   "source": [
    "prompt =f\"\"\" \n",
    "你是一位客户服务的AI助手。\n",
    "你的任务是给一位重要的客户发送邮件回复。\n",
    "通过客户评论，生产回复客户的评价;如果客户评论情感是积极的，回复感谢客户的关注; 如果客户回复是负面的，告诉客户可以联系客户反馈遇到的问题。\n",
    "提醒模型使用评价中的具体细节用简明而专业的语气写信。\n",
    "作为\"AI客户代理\"签署电子邮件。\n",
    "客户评论：\n",
    "```{review}```\n",
    "评论情感：{sentiment}\n",
    "\n",
    "\"\"\"\n",
    "response = get_completion(prompt,temperature=0.7)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ce7e928-4272-410f-8120-9c40e3522e9e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
